package net.maergaiyun.config;


import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
@Data
public class RabbitMQConfig {
    /**
     * 交换机
     */
    private String orderEventExchange = "order.event.exchange";

    /**
     * 延迟队列, 不能被消费者监听
     */
    private String orderCloseDelayQueue = "order.close.delay.queue";

    /**
     * 关单队列, 延迟队列的消息过期后转发的队列, 用于被消费者监听
     */
    private String orderCloseQueue = "order.close.queue";

    /**
     * 进入延迟队列的routingKey
     */
    private String orderCloseDelayRoutingKey = "order.close.delay.routing.key";

    /**
     * 进入死信队列的routingKey
     */
    private String orderCloseRoutingKey = "order.close.routing.key";

    /**
     * 过期时间, 毫秒单位, 开发阶段改为1分钟
     */
    private Integer ttl = 1000 * 60;

    /**
     * 消息转换器
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 创建交换机, topic类型, 一般一个业务一个交换机
     *
     * @return
     */
    @Bean
    public Exchange orderEventChange() {
        return new TopicExchange(orderEventExchange, true, false);
    }

    /**
     * 延迟队列
     *
     * @return
     */
    @Bean
    public Queue orderCloseDelayQueue() {
        HashMap<String, Object> args = new HashMap<>(3);
        args.put("x-dead-letter-exchange", orderEventExchange);
        args.put("x-dead-letter-routing-key", orderCloseRoutingKey);
        args.put("x-message-ttl", ttl);
        return new Queue(orderCloseDelayQueue, true, false, false, args);
    }

    /**
     * 死信队列, 用于被监听
     *
     * @return
     */
    @Bean
    public Queue orderCloseQueue() {
        return new Queue(orderCloseQueue, true, false, false);
    }

    /**
     * 延迟队列和交换机建立绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderCloseDelayBinding() {
        return new Binding(orderCloseDelayQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderCloseDelayRoutingKey, null);
    }

    /**
     * 死信队列和交换机建立绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderCloseBinding() {
        return new Binding(orderCloseQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderCloseRoutingKey, null);
    }


    // 订单支付成功配置
    /**
     * 更新订单队列
     */
    private String orderUpdateQueue = "order.update.queue";

    /**
     * 发放流量包队列
     */
    private String orderTrafficQueue = "order.traffic.queue";

    /**
     * 微信回调发送通知routing key
     */
    private String orderUpdateTrafficRoutingKey = "order.update.traffic.routing.key";

    /**
     * 绑定更新订单队列和交换机
     */
    private String orderUpdateBindingKey = "order.update.*.routing.key";

    /**
     * 绑定发送流量包队列和交换机
     */
    private String orderTrafficBindingKey = "order.*.traffic.routing.key";

    /**
     * 更新订单队列和交换机建立绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderUpdateBinding() {
        return new Binding(orderUpdateQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderUpdateBindingKey, null);
    }

    /**
     * 发送流量包队列和交换机建立绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderTrafficBinding() {
        return new Binding(orderTrafficQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderTrafficBindingKey, null);
    }

    /**
     * 更新订单队列
     *
     * @return
     */
    @Bean
    public Queue orderUpdateQueue() {
        return new Queue(orderUpdateQueue, true, false, false);
    }

    /**
     * 发送流量包队列
     *
     * @return
     */
    @Bean
    public Queue orderTrafficQueue() {
        return new Queue(orderTrafficQueue, true, false, false);
    }
}
